package cn.wenhe9.yygh.hosp.controller.api;

import cn.wenhe9.yygh.common.result.ResultResponse;
import cn.wenhe9.yygh.hosp.service.DepartmentService;
import cn.wenhe9.yygh.hosp.service.HospitalService;
import cn.wenhe9.yygh.hosp.service.HospitalSetService;
import cn.wenhe9.yygh.hosp.service.ScheduleService;
import cn.wenhe9.yygh.hosp.utils.HttpRequestHelper;
import cn.wenhe9.yygh.model.hosp.Hospital;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author DuJinliang
 * 2022/2/18
 */
@Api(tags = "医院数据接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Resource
    private HospitalService hospitalService;

    @Resource
    private HospitalSetService hospitalSetService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private ScheduleService scheduleService;

    /**
     * 查询医院
     * @param request HttpServletRequest
     * @return ResultResponse
     */
    @ApiOperation("查询医院接口")
    @PostMapping("/show")
    public ResultResponse getHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        hospitalSetService.checkSignKey(paramMap);

        String hoscode = (String) paramMap.get("hoscode");

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        if (hospital == null){
            return ResultResponse.fail();
        }
        return ResultResponse.ok(hospital);
    }

    /**
     * 上传医院接口
     * @param request HttpServletRequest
     * @return ResultResponse
     * @throws JsonProcessingException json转换异常
     */
    @ApiOperation("上传医院接口")
    @PostMapping("/save")
    public ResultResponse saveHosp(HttpServletRequest request) throws JsonProcessingException {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        hospitalSetService.checkSignKey(paramMap);

        //传输过程中， "+" 转换为了 " "，因此需要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        return hospitalService.saveHosp(paramMap);
    }

    @ApiOperation("上传科室接口")
    @PostMapping("/dep/save")
    public ResultResponse saveDepartment(HttpServletRequest request) throws JsonProcessingException {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        hospitalSetService.checkSignKey(paramMap);

        return departmentService.save(paramMap);
    }

    @ApiOperation("查询科室接口")
    @PostMapping("/dep/show")
    public ResultResponse findDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        hospitalSetService.checkSignKey(paramMap);

        //医院的编号
        String hoscode = (String) paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 5 : Integer.parseInt((String) paramMap.get("limit"));

        return departmentService.findDepartmentByPage(hoscode, page, limit);
    }

    @ApiOperation("删除科室接口")
    @PostMapping("/dep/remove")
    public ResultResponse removeDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        hospitalSetService.checkSignKey(paramMap);

        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        return departmentService.logicDelete(hoscode, depcode);
    }

    @ApiOperation("上传排班接口")
    @PostMapping("/schedule/save")
    public ResultResponse saveSchedule(HttpServletRequest request) throws JsonProcessingException {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        hospitalSetService.checkSignKey(paramMap);

        return scheduleService.save(paramMap);
    }

    @ApiOperation("查询排班接口")
    @PostMapping("/schedule/show")
    public ResultResponse findSchedules(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        hospitalSetService.checkSignKey(paramMap);

        String hoscode = (String) paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 5 : Integer.parseInt((String) paramMap.get("limit"));

        return scheduleService.findScheduleByPage(hoscode, page, limit);
    }

    @ApiOperation("删除排班接口")
    @PostMapping("/schedule/remove")
    public ResultResponse remvoeSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        hospitalSetService.checkSignKey(paramMap);

        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        scheduleService.logicDelete(hoscode, hosScheduleId);

        return ResultResponse.ok();
    }

}
